<?php
include dirname(dirname(__FILE__)) . '/conf.php';
include SERVER_ROOT . 'public/saetv2.ex.class.php';

$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );

if (isset($_REQUEST['code'])) {
	$keys = array();
	$keys['code'] = $_REQUEST['code'];
	$keys['redirect_uri'] = WB_CALLBACK_URL;
	try {
		$token = $o->getAccessToken( 'code', $keys ) ;
	} catch (OAuthException $e) {
	}
}

if ($token) {
	$_SESSION['token'] = $token;
	setcookie( 'weibojs_'.$o->client_id, http_build_query($token) );

	echo 'Successfully!';
} else {
	echo 'Failed!';
}


/*
$_SESSION = 
array(1) {
  ["token"]=>
  &array(4) {
    ["access_token"]=>
    string(32) "2.00OAHJTBjiqlkD9cfe318aebpTGuTE"
    ["expires_in"]=>
    int(86400)
    ["remind_in"]=>
    string(5) "86035"
    ["uid"]=>
    string(10) "1346818450"
  }
}

$_COOKIE = 
array(3) {
  ["__utma"]=>
  string(55) "103187593.2140473283.1331373777.1331373777.1331373777.1"
  ["__utmz"]=>
  string(70) "103187593.1331373777.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)"
  ["PHPSESSID"]=>
  string(26) "1jlrslj1fvuvfrgrfh3dgsdk33"
}
*/

?>